<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="dianji">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->

      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>

          </template>
        </el-table-column>
        <el-table-column align="center" label="描述">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150" fixed="right">
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="queren(row)">确认</el-button>
              <el-button type="text" size="small" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="hshow(row)">分配权限</el-button>
              <el-button type="text" size="small" @click="bianji(row)">编辑</el-button>
              <template>
                <el-popconfirm
                  confirm-button-text="好的"
                  cancel-button-text="不用了"
                  icon="el-icon-info"
                  icon-color="red"
                  title="这是一段内容确定删除吗？"
                  @onConfirm="dell(row.id)"
                >
                  <el-button slot="reference" size="small" type="text">删除</el-button>
                </el-popconfirm>
              </template>
            </template>

          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="page"
          background
          layout="sizes, prev, pager, next"
          :total="total"
          :page-size="pagesize"
          :page-sizes="[3,5,7]"
          @size-change="sChange"
          @current-change="hChange"
        />
      </el-row>
    </div>
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch
            v-model="roleForm.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            size="mini"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="add">确定</el-button>
              <el-button size="mini">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <zujian
      ref="diaoyong"
      :show.sync="show"
      :curid="curid"
    />
  </div>
</template>
<script>
import { delllx, upRole, getRoles, getRolesdata } from '@/api/user'
import { Message } from 'element-ui'
import zujian from './zujian.vue'

export default {
  name: 'Role',
  components: { zujian },
  data() {
    return {
      curid: -1,
      show: false,
      roleForm: {
        name: '',
        description: '',
        state: 1 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showDialog: false,
      pagesize: 5,
      list: [],
      total: 1,
      page: 1
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    hshow(row) {
      this.show = true
      console.log(row.id)

      this.curid = row.id

      this.$nextTick(() => {
        this.$refs.diaoyong.quanxianguanli()
      })
    },
    async dell(id) {
      // console.log(id)
      const res = await delllx(id)
      console.log(res)

      if (this.list.length === 1) {
        this.page--
      }
      this.loadRoleList()
      Message('删除成功')
    },
    async  queren(row) {
      // console.log(row)
      const res = await upRole(row.editRow)
      console.log(res)
      this.loadRoleList()
      // eslint-disable-next-line require-atomic-updates
      row.isEdit = false
    },
    bianji(row) {
      console.log(row)
      row.isEdit = true
    },
    async hadd() {
    // console.log()
      const res = await getRolesdata(this.roleForm)
      console.log(res)
      this.showDialog = false
      this.$message.success('新增角色成功')
      this.loadRoleList()
    },
    add() {
      this.$refs.roleForm.validate(async isOK => {
        if (isOK) {
          this.hadd()
        }
      })
    },
    dianji() {
      this.showDialog = true
    },
    sChange(i) {
      console.log(i)
      this.pagesize = i
      this.loadRoleList()
    },
    hChange(i) {
    // console.log(i)
      this.page = i
      this.loadRoleList()
    },
    async  loadRoleList() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })

      this.list = res.data.rows
      this.total = res.data.total
      console.log(res.data.rows)
      this.list.forEach(it => {
        this.$set(it, 'isEdit', false)
        this.$set(it, 'editRow', {
          name: it.name,
          state: it.state,
          description: it.description,
          id: it.id
        })
      })
    }
  }

}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
